from twisted.spread import pb
from twisted.internet import reactor
from twisted.python import util
from twisted.cred import credentials

from option import Option

class NetMind(pb.Referenceable):
	def __init__(self):
		self.obj = None
		self.data = None
	
	def connectionLost(self, obj):
		Option().tabNet.connectionLost(self.data)
		del self.obj
		del self

	def connected(self, obj, data):
		self.obj = obj
		self.data = data

		obj.notifyOnDisconnect(self.connectionLost)
		Option().tabNet.connectionMade(obj, data)
		Option().netMind = self
		return obj

	def remote_send(self, msg):
		Option().tabNet.chat(*msg.split(':',1))

	def getEventsInfo(self, callback):
		self.obj.callRemote('getEventsInfo').addCallback(callback)

	def remote_notify(self, param1, param2):
		if param1 == 'leave':
			Option().tabNet.users.remove(param2)
			Option().tabNet.populateUserList()
		elif param1 == 'enter':
			Option().tabNet.users.append(param2)
			Option().tabNet.populateUserList()


